// const BASEURL = 'https://www.uinav.com/api/public/v1/'
const BASEURL = 'https://api-hmugo-web.itheima.net/api/public/v1/'

function request ({
  url,
  method = 'GET',
  header = {},
  data = {},
  tip = '拼命加载中...',
  isPullDown = false
}) {
  return new Promise((resolve, reject) => {
    // 开启loading效果
    if (!isPullDown) {
      uni.showLoading({
        title: tip,
        mask: true
      })
    }

    uni.request({
      url: `${BASEURL}${url}`,
      method,
      data,
      header,
      success: ({ data, statusCode, header }) => {
        resolve(data)
      },
      fail: error => {
        reject(error)
      },
      complete: () => {
        if (!isPullDown) {
          uni.hideLoading()
        }
      }
    })
  })
}

uni.$request = request
